﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;

namespace MockingBird.Simulator.Framework.Configuration
{
    [Serializable]
    public class HandlerMapInfo
    {
        private List<HandlerMapEntry> entries;

        public bool IsEmpty
        {
            get
            {
                if (this.entries.Count == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public HandlerMapInfo()
        {
            this.entries = new List<HandlerMapEntry>();
        }

        public List<HandlerMapEntry> GetEntries()
        {
            return this.entries;
        }



        public void LoadInfoFromXmlDocument(XmlDocument xmlDocument)
        {
            this.entries = new List<HandlerMapEntry>();

            XmlNodeList nodeList = xmlDocument.SelectNodes("//MapEntry");
            if (nodeList != null)
            {
                foreach (XmlNode node in nodeList)
                {
                    HandlerMapEntry newEntry = new HandlerMapEntry();
                    newEntry.Endpoint = node.Attributes["endpoint"].InnerText;
                    newEntry.HandlerName = node.Attributes["handler"].InnerText;
                    if (!this.EntryExists(newEntry))
                    {
                        this.entries.Add(newEntry);
                    }


                }
            }
        }

        public void AddEntry(HandlerMapEntry newEntry)
        {
            if (!this.EntryExists(newEntry))
            {
                this.entries.Add(newEntry);
            }
        }

        public void LoadInfoFromXml(string xml)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xml);
            this.LoadInfoFromXmlDocument(xmlDocument);
        }

        public string GetHandlerNameForEndpoint(string endpoint)
        {
            string result = string.Empty;

            try
            {
                result = this.entries.Find(x => x.Endpoint.ToLower().Equals(endpoint.ToLower())).HandlerName;
            }
            catch (Exception nullEx)
            {

            }
            return result;
        }

        public bool EntryExists(HandlerMapEntry candidate)
        {
            return this.entries.Exists(e => e.Equals(candidate));
        }

        public string Serialize()
        {
            return this.SerializeToXElement().ToString();



        }

        public XElement SerializeToXElement()
        {
            XElement handlerMapElement = new XElement("HandlerMap");
            foreach (HandlerMapEntry entry in this.entries)
            {
                XElement entryElement = new XElement("MapEntry");
                XAttribute endpointAttribute = new XAttribute("endpoint", entry.Endpoint);
                XAttribute handlerAttribute = new XAttribute("handler", entry.HandlerName);
                entryElement.Add(endpointAttribute, handlerAttribute);
                handlerMapElement.Add(entryElement);
            }
            return handlerMapElement;

        }




    }
}
